home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2001 January / Game.EXE_01_2001.iso / demos / Blade of Darkness / data1.cab / Program_Executable_Files / Lib / Objects.py < prev    next >
Encoding:
Python Source  |  2000-11-16  |  15.3 KB  |  468 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. import Bladex
  7. import InitDataField
  8. import B3DLib
  9. import GameStateAux
  10. import pdb
  11. import ObjStore
  12.  
  13. REL=0
  14. ABS=1
  15.  
  16.  
  17.  
  18.  
  19. def DisplaceAndRotateObjectFunc(obj_name, time):
  20.  
  21.     obj=Bladex.GetEntity(obj_name)
  22.  
  23.     dinobj=obj.Data.dinobjdata
  24.  
  25.     if (dinobj.OnMovement==0 and dinobj.OnRotation==0):
  26.         obj.RemoveFromList(dinobj.Timer)
  27.         obj.TimerFunc=""
  28.         return
  29.  
  30.     if (dinobj.OnMovement==1):
  31.         if (time<dinobj.end_time):
  32.             itime=time-dinobj.last_time
  33.             dinobj.displ=dinobj.init_vel*itime+(dinobj.acc*(itime**2))/2.0
  34.             x=dinobj.displ_vector[0]*dinobj.displ
  35.             y=dinobj.displ_vector[1]*dinobj.displ
  36.             z=dinobj.displ_vector[2]*dinobj.displ
  37.             obj.Move(x,y,z)
  38.             iend_vel=dinobj.init_vel+dinobj.acc*itime
  39.             dinobj.init_vel=iend_vel
  40.             dinobj.last_time=time
  41.             if (dinobj.while_displ_sound):
  42.                 dinobj.while_displ_sound.Position=dinobj.obj.Position
  43.             if (dinobj.WhileDisplFunc):
  44.                 apply(dinobj.WhileDisplFunc, dinobj.WhileDisplArgs)
  45.         else:
  46.             itime=dinobj.end_time-dinobj.last_time
  47.             dinobj.displ=dinobj.init_vel*itime+(dinobj.acc*(itime**2))/2.0
  48.             x=dinobj.displ_vector[0]*dinobj.displ
  49.             y=dinobj.displ_vector[1]*dinobj.displ
  50.             z=dinobj.displ_vector[2]*dinobj.displ
  51.             obj.Move(x,y,z)
  52.             dinobj.OnMovement=0
  53.             if (dinobj.while_displ_sound and (dinobj.while_displ_sound!=dinobj.next_while_displ_sound or dinobj.last_displ)):
  54.                 dinobj.while_displ_sound.StopSound()
  55.             if (dinobj.end_displ_sound):
  56.                 dinobj.end_displ_sound.Position=dinobj.obj.Position
  57.                 dinobj.end_displ_sound.PlaySound(0)
  58.             if (dinobj.OnRotation==0):
  59.                 obj.RemoveFromList(dinobj.Timer)
  60.                 obj.TimerFunc=""
  61.             if (dinobj.EndDisplFunc):
  62.                 apply(dinobj.EndDisplFunc, dinobj.EndDisplArgs)
  63.  
  64.     if (dinobj.OnRotation==1):
  65.         if (time<dinobj.end_time_w):
  66.             itime=time-dinobj.last_time_w
  67.             dinobj.angle=dinobj.init_w*itime+(dinobj.acc_w*(itime**2))/2.0
  68.             ce=dinobj.center
  69.             ax=dinobj.axis
  70.             if (dinobj.rotation_type==REL):
  71.                 obj.RotateRel(ce[0], ce[1], ce[2], ax[0], ax[1], ax[2], dinobj.angle)
  72.             else:
  73.                 obj.RotateAbs(ce[0], ce[1], ce[2], ax[0], ax[1], ax[2], dinobj.angle)
  74.             iend_w=dinobj.init_w+dinobj.acc_w*itime
  75.             dinobj.init_w=iend_w
  76.             dinobj.last_time_w=time
  77.             if (dinobj.while_rot_sound):
  78.                 dinobj.while_rot_sound.Position=dinobj.obj.Position
  79.             if (dinobj.WhileRotFunc):
  80.                 apply(dinobj.WhileRotFunc, dinobj.WhileRotArgs)
  81.         else:
  82.             itime=dinobj.end_time_w-dinobj.last_time_w
  83.             dinobj.angle=dinobj.init_w*itime+(dinobj.acc_w*(itime**2))/2.0
  84.             ce=dinobj.center
  85.             ax=dinobj.axis
  86.             if (dinobj.rotation_type==REL):
  87.                 obj.RotateRel(ce[0], ce[1], ce[2], ax[0], ax[1], ax[2], dinobj.angle)
  88.             else:
  89.                 obj.RotateAbs(ce[0], ce[1], ce[2], ax[0], ax[1], ax[2], dinobj.angle)
  90.             dinobj.OnRotation=0
  91.             if (dinobj.while_rot_sound and (dinobj.while_rot_sound!=dinobj.next_while_rot_sound or dinobj.last_rot)):
  92.                 dinobj.while_rot_sound.StopSound()
  93.             if (dinobj.end_rot_sound):
  94.                 dinobj.end_rot_sound.Position=dinobj.obj.Position
  95.                 dinobj.end_rot_sound.PlaySound(0)
  96.             if (dinobj.OnMovement==0):
  97.                 obj.RemoveFromList(dinobj.Timer)
  98.                 obj.TimerFunc=""
  99.             if (dinobj.EndRotFunc):
  100.                 apply(dinobj.EndRotFunc, dinobj.EndRotArgs)
  101.  
  102.  
  103.  
  104.  
  105.  
  106. ##class DinObj(GameStateAux.PersistentObject):
  107. class DinObj:
  108.  
  109.     ObjId=""
  110.     Activado=0
  111.     OnMovement=0
  112.     OnRotation=0
  113.     prev_while_displ_sound=None
  114.     while_displ_sound=None
  115.     next_while_displ_sound=None
  116.     end_displ_sound=None
  117.     current_displ=0
  118.     last_displ=1
  119.     prev_while_rot_sound=None
  120.     while_rot_sound=None
  121.     next_while_rot_sound=None
  122.     end_rot_sound=None
  123.     last_rot=1
  124.     Timer="Timer60"
  125.     obj=None
  126.  
  127.     displ_vector=(0,0,0)
  128.     displ=0
  129.     end_time=0
  130.     last_time=0
  131.     init_vel=0
  132.     acc=0
  133.     init_w=0
  134.     acc_w=0
  135.     center=(0,0,0)
  136.     axis=(0,0,0)
  137.     rotation_type=0
  138.     end_time_w=0
  139.     last_time_w=0
  140.  
  141.     EndDisplFunc=None
  142.     EndDisplArgs=None
  143.     EndRotFunc=None
  144.     EndRotArgs=None
  145.     
  146.     WhileDisplFunc=None
  147.     WhileDisplArgs=None
  148.     WhileRotFunc=None
  149.     WhileRotArgs=None
  150.  
  151.  
  152.  
  153.     def __init__(self):
  154. ##        GameStateAux.PersistentObject.__init__(self)
  155.         self.ObjId=ObjStore.GetNewId()
  156.         ObjStore.ObjectsStore[self.ObjId]=self
  157.  
  158.     def __del__(self):
  159.         del ObjStore.ObjectsStore[self.ObjId]
  160.  
  161.     def DisplaceDinObj(self):
  162.  
  163.         self.OnMovement=1
  164.         if (self.OnRotation==0):
  165.             self.obj.TimerFunc=DisplaceAndRotateObjectFunc
  166.             self.obj.SubscribeToList(self.Timer)
  167.  
  168.  
  169.     def RotateDinObj(self):
  170.  
  171.         self.OnRotation=1
  172.         if (self.OnMovement==0):
  173.             self.obj.TimerFunc=DisplaceAndRotateObjectFunc
  174.             self.obj.SubscribeToList(self.Timer)
  175.  
  176.     def Stop(self):
  177.  
  178.         self.OnMovement=0
  179.         self.OnRotation=0
  180.  
  181.     def StopOnStep(self):
  182.  
  183.         self.EndDisplFunc=None
  184.         self.EndRotFunc=None
  185.  
  186.     def StopDisplacement(self):
  187.  
  188.         self.OnMovement=0
  189.  
  190.     def StopDisplacementOnStep(self):
  191.  
  192.         self.EndDisplFunc=None
  193.  
  194.     def StopRotation(self):
  195.  
  196.         self.OnRotation=0
  197.  
  198.     def StopRotationOnStep(self):
  199.  
  200.         self.EndRotFunc=None
  201.  
  202.     def persistent_id(self):
  203.         return self.ObjId
  204.  
  205.  
  206.     def __getstate__(self):
  207.         # Tiene que devolver como poder guardar el estado de la clase
  208.  
  209.         return (1,
  210.                 self.ObjId,
  211.                 self.Activado,
  212.                 self.OnMovement,
  213.                 self.OnRotation,
  214.                 GameStateAux.SaveEntityAux(self.prev_while_displ_sound),
  215.                 GameStateAux.SaveEntityAux(self.while_displ_sound),
  216.                 GameStateAux.SaveEntityAux(self.next_while_displ_sound),
  217.                 self.last_displ,
  218.                 GameStateAux.SaveEntityAux(self.prev_while_rot_sound),
  219.                 GameStateAux.SaveEntityAux(self.while_rot_sound),
  220.                 GameStateAux.SaveEntityAux(self.next_while_rot_sound),
  221.                 self.last_rot,
  222.                 self.Timer,
  223.                 GameStateAux.SaveEntityAux(self.obj),
  224.                 self.displ_vector,
  225.                 self.displ,
  226.                 self.end_time,
  227.                 self.last_time,
  228.                 self.init_vel,
  229.                 self.acc,
  230.                 self.init_w,
  231.                 self.acc_w,
  232.                 self.center,
  233.                 self.axis,
  234.                 self.rotation_type,
  235.                 self.end_time_w,
  236.                 self.last_time_w,
  237.                 GameStateAux.SaveFunctionAux(self.EndDisplFunc),
  238.                 self.EndDisplArgs,
  239.                 GameStateAux.SaveFunctionAux(self.EndRotFunc),
  240.                 self.EndRotArgs,
  241.                 GameStateAux.SaveFunctionAux(self.WhileDisplFunc),
  242.                 self.WhileDisplArgs,
  243.                 GameStateAux.SaveFunctionAux(self.WhileRotFunc),
  244.                 self.WhileRotArgs,
  245.                 GameStateAux.SaveEntityAux(self.end_displ_sound),
  246.                 self.current_displ,
  247.                 GameStateAux.SaveNewMembers(self)
  248.                 )
  249.  
  250.     def __setstate__(self,parm):
  251.         # Toma como parßmetro lo que devuelve __getstate__() y debe recrear la clase
  252.  
  253.         if parm[0]==1:
  254.             #self.ObjId=parm[1] En GameStateAux.PersistentObject()
  255.             self.ObjId=parm[1]
  256.             ObjStore.ObjectsStore[self.ObjId]=self
  257.             self.Activado=parm[2]
  258.             self.OnMovement=parm[3]
  259.             self.OnRotation=parm[4]
  260.  
  261.             self.prev_while_displ_sound=GameStateAux.LoadEntityAux(parm[5])
  262.             self.while_displ_sound=GameStateAux.LoadEntityAux(parm[6])
  263.             self.next_while_displ_sound=GameStateAux.LoadEntityAux(parm[7])
  264.  
  265.             self.last_displ=parm[8]
  266.             
  267.             self.prev_while_rot_sound=GameStateAux.LoadEntityAux(parm[9])
  268.             self.while_rot_sound=GameStateAux.LoadEntityAux(parm[10])
  269.             self.next_while_rot_sound=GameStateAux.LoadEntityAux(parm[11])
  270.  
  271.             self.last_rot=parm[12]
  272.             self.Timer=parm[13]
  273.             self.obj=GameStateAux.LoadEntityAux(parm[14])
  274.  
  275.             self.displ_vector=parm[15]
  276.             self.displ=parm[16]
  277.             self.end_time=parm[17]
  278.             self.last_time=parm[18]
  279.             self.init_vel=parm[19]
  280.             self.acc=parm[20]
  281.             self.init_w=parm[21]
  282.             self.acc_w=parm[22]
  283.             self.center=parm[23]
  284.             self.axis=parm[24]
  285.             self.rotation_type=parm[25]
  286.             self.end_time_w=parm[26]
  287.             self.last_time_w=parm[27]
  288.  
  289.             GameStateAux.LoadFunctionAux(parm[28],self,"EndDisplFunc")
  290.             self.EndDisplArgs=parm[29]
  291.             GameStateAux.LoadFunctionAux(parm[30],self,"EndRotFunc")
  292.             self.EndRotArgs=parm[31]
  293.             GameStateAux.LoadFunctionAux(parm[32],self,"WhileDisplFunc")
  294.             self.WhileDisplArgs=parm[33]
  295.             GameStateAux.LoadFunctionAux(parm[34],self,"WhileRotFunc")
  296.             self.WhileRotArgs=parm[35]
  297.             self.end_displ_sound=GameStateAux.LoadEntityAux(parm[36])
  298.             self.current_displ=parm[37]
  299.  
  300.             GameStateAux.LoadNewMembers(self,parm[38])
  301.  
  302.             if self.obj:
  303.                 self.obj.Data=self
  304.         else:
  305.             print "DinObj.__setstate__() -> Version mismatch"
  306.             self.ObjId=ObjStore.GetNewId()
  307.             ObjStore.ObjectsStore[self.ObjId]=self
  308.  
  309.  
  310.  
  311.  
  312. def DisplaceObject(dinobj, displ, displ_vector, init_vel, end_vel, init_displ_sound="", while_displ_sound="", end_displ_sound="", WhileDisplFunc="", WhileDisplArgs=(), EndDisplFunc="", EndDisplArgs=()):
  313.  
  314.     dinobj.displ=displ
  315.     dinobj.displ_vector=B3DLib.Normalize(displ_vector)
  316.     dinobj.init_vel=float(init_vel)
  317.     dinobj.init_displ_sound=init_displ_sound
  318.     dinobj.while_displ_sound=while_displ_sound
  319.     dinobj.end_displ_sound=end_displ_sound
  320.     dinobj.WhileDisplFunc=WhileDisplFunc
  321.     dinobj.WhileDisplArgs=WhileDisplArgs
  322.     dinobj.EndDisplFunc=EndDisplFunc
  323.     dinobj.EndDisplArgs=EndDisplArgs
  324.     dinobj.acc=(end_vel**2-init_vel**2)/(2.0*displ)
  325.     if (dinobj.acc==0.0):
  326.         displ_time=displ/float(init_vel)
  327.     else:
  328.         displ_time=(end_vel-init_vel)/dinobj.acc
  329.     if (dinobj.init_displ_sound):
  330.         dinobj.init_displ_sound.Position=dinobj.obj.Position
  331.         dinobj.init_displ_sound.PlaySound(0)
  332.     if (dinobj.while_displ_sound and dinobj.while_displ_sound!=dinobj.prev_while_displ_sound):
  333.         dinobj.while_displ_sound.Position=dinobj.obj.Position
  334.         dinobj.while_displ_sound.PlaySound(-1)
  335.     start_time=Bladex.GetTime()
  336.     dinobj.last_time=Bladex.GetTime()
  337.     dinobj.end_time=start_time+displ_time
  338.     dinobj.DisplaceDinObj()
  339.  
  340.  
  341. def DisplaceObjectFromTo(dinobj, init_point, end_point, init_vel, end_vel, init_displ_sound="", while_displ_sound="", end_displ_sound="", WhileDisplFunc="", WhileDisplArgs=(), EndDisplFunc="", EndDisplArgs=()):
  342. ##    pdb.set_trace()
  343.     displ_vector=end_point[0]-init_point[0], end_point[1]-init_point[1], end_point[2]-init_point[2]
  344.     displ=(displ_vector[0]**2+displ_vector[1]**2+displ_vector[2]**2)**0.5
  345.     dinobj.obj.Position=init_point
  346.     DisplaceObject(dinobj, displ, displ_vector, init_vel, end_vel, init_displ_sound, while_displ_sound, end_displ_sound, WhileDisplFunc, WhileDisplArgs, EndDisplFunc, EndDisplArgs)
  347.  
  348.  
  349. def RotateObject(dinobj, angle, init_w, end_w, center, axis, rotation_type=REL, init_rot_sound="", while_rot_sound="", end_rot_sound="", WhileRotFunc="", WhileRotArgs=(), EndRotFunc="", EndRotArgs=()):
  350.  
  351.     dinobj.angle=angle
  352.     if (angle<0):
  353.         init_w=-init_w
  354.         end_w=-end_w
  355.     dinobj.init_w=float(init_w)
  356.     dinobj.center=center
  357.     dinobj.axis=axis
  358.     dinobj.rotation_type=rotation_type
  359.     dinobj.init_rot_sound=init_rot_sound
  360.     dinobj.while_rot_sound=while_rot_sound
  361.     dinobj.end_rot_sound=end_rot_sound
  362.     dinobj.WhileRotFunc=WhileRotFunc
  363.     dinobj.WhileRotArgs=WhileRotArgs
  364.     dinobj.EndRotFunc=EndRotFunc
  365.     dinobj.EndRotArgs=EndRotArgs
  366.     dinobj.acc_w=(end_w**2-init_w**2)/(2.0*angle)
  367.     if (dinobj.acc_w==0.0):
  368.         rotat_time=angle/float(init_w)
  369.     else:
  370.         rotat_time=(end_w-init_w)/dinobj.acc_w
  371.     if (dinobj.init_rot_sound):
  372.         dinobj.init_rot_sound.Position=dinobj.obj.Position
  373.         dinobj.init_rot_sound.PlaySound(0)
  374.     if (dinobj.while_rot_sound and dinobj.while_rot_sound!=dinobj.prev_while_rot_sound):
  375.         dinobj.while_rot_sound.Position=dinobj.obj.Position
  376.         dinobj.while_rot_sound.PlaySound(-1)
  377.     start_time=Bladex.GetTime()
  378.     dinobj.last_time_w=Bladex.GetTime()
  379.     dinobj.end_time_w=start_time+rotat_time
  380.     dinobj.RotateDinObj()
  381.  
  382.  
  383. def NDisplacement(dinobj, displ, displ_vector, init_vel, end_vel, init_displ_sound, while_displ_sound, end_displ_sound):
  384.  
  385.     dinobj.currentdispl=dinobj.currentdispl+1
  386.     n=dinobj.currentdispl
  387.     dinobj.prev_while_displ_sound=while_displ_sound[n-1]
  388.     if (n<dinobj.ndispl-1):
  389.         dinobj.next_while_displ_sound=while_displ_sound[n+1]
  390.         DisplaceObject(dinobj, displ[n], displ_vector[n], init_vel[n], end_vel[n], init_displ_sound[n], while_displ_sound[n], end_displ_sound[n], dinobj.WhileDisplFunc, dinobj.WhileDisplArgs, NDisplacement, (dinobj, displ, displ_vector, init_vel, end_vel, init_displ_sound, while_displ_sound, end_displ_sound))
  391.     else:
  392.         dinobj.next_while_displ_sound=""
  393.         dinobj.last_displ=1
  394.         DisplaceObject(dinobj, displ[n], displ_vector[n], init_vel[n], end_vel[n], init_displ_sound[n], while_displ_sound[n], end_displ_sound[n], dinobj.WhileDisplFunc, dinobj.WhileDisplArgs, dinobj.EndNDisplFunc, dinobj.EndNDisplArgs)
  395.  
  396.  
  397. def NDisplaceObject(dinobj, displ, displ_vector, init_vel, end_vel, init_displ_soundlist=(), while_displ_soundlist=(), end_displ_soundlist=(), WhileDisplFunc="", WhileDisplArgs=(), EndDisplFunc="", EndDisplArgs=()):
  398.  
  399.     init_displ_sound=[]
  400.     while_displ_sound=[]
  401.     end_displ_sound=[]
  402.     for n in range(len(displ)):
  403.         init_displ_sound.append("")
  404.         while_displ_sound.append("")
  405.         end_displ_sound.append("")
  406.     for n in range(len(init_displ_soundlist)):
  407.         init_displ_sound[n]=init_displ_soundlist[n]
  408.     for n in range(len(while_displ_soundlist)):
  409.         while_displ_sound[n]=while_displ_soundlist[n]
  410.     for n in range(len(end_displ_soundlist)):
  411.         end_displ_sound[n]=end_displ_soundlist[n]
  412.     dinobj.EndNDisplFunc=EndDisplFunc
  413.     dinobj.EndNDisplArgs=EndDisplArgs
  414.     dinobj.ndispl=len(displ)
  415.     dinobj.currentdispl=0
  416.     dinobj.prev_while_displ_sound=""
  417.     dinobj.next_while_displ_sound=while_displ_sound[1]
  418.     dinobj.last_displ=0
  419.     DisplaceObject(dinobj, displ[0], displ_vector[0], init_vel[0], end_vel[0], init_displ_sound[0], while_displ_sound[0], end_displ_sound[0], WhileDisplFunc, WhileDisplArgs, NDisplacement, (dinobj, displ, displ_vector, init_vel, end_vel, init_displ_sound, while_displ_sound, end_displ_sound))
  420.  
  421.  
  422. def NRotation(dinobj, angle, init_w, end_w, center, axis, init_rot_sound, while_rot_sound, end_rot_sound):
  423.  
  424.     dinobj.currentangle=dinobj.currentangle+1
  425.     n=dinobj.currentangle
  426.     dinobj.prev_while_rot_sound=while_rot_sound[n-1]
  427.     if (n<dinobj.nangle-1):
  428.         dinobj.next_while_rot_sound=while_rot_sound[n+1]
  429.         RotateObject(dinobj, angle[n], init_w[n], end_w[n], center[n], axis[n], dinobj.rotation_type, init_rot_sound[n], while_rot_sound[n], end_rot_sound[n], dinobj.WhileRotFunc, dinobj.WhileRotArgs, NRotation, (dinobj, angle, init_w, end_w, center, axis, init_rot_sound, while_rot_sound, end_rot_sound))
  430.     else:
  431.         dinobj.next_while_rot_sound=""
  432.         dinobj.last_rot=1
  433.         RotateObject(dinobj, angle[n], init_w[n], end_w[n], center[n], axis[n], dinobj.rotation_type, init_rot_sound[n], while_rot_sound[n], end_rot_sound[n], dinobj.WhileRotFunc, dinobj.WhileRotArgs, dinobj.EndNRotFunc, dinobj.EndNRotArgs)
  434.  
  435.  
  436. def NRotateObject(dinobj, angle, init_w, end_w, center, axis, rotation_type=REL, init_rot_soundlist=(), while_rot_soundlist=(), end_rot_soundlist=(), WhileRotFunc="", WhileRotArgs=(), EndRotFunc="", EndRotArgs=()):
  437.  
  438.     init_rot_sound=[]
  439.     while_rot_sound=[]
  440.     end_rot_sound=[]
  441.     for n in range(len(angle)):
  442.         init_rot_sound.append("")
  443.         while_rot_sound.append("")
  444.         end_rot_sound.append("")
  445.     for n in range(len(init_rot_soundlist)):
  446.         init_rot_sound[n]=init_rot_soundlist[n]
  447.     for n in range(len(while_rot_soundlist)):
  448.         while_rot_sound[n]=while_rot_soundlist[n]
  449.     for n in range(len(end_rot_soundlist)):
  450.         end_rot_sound[n]=end_rot_soundlist[n]
  451.     dinobj.EndNRotFunc=EndRotFunc
  452.     dinobj.EndNRotArgs=EndRotArgs
  453.     dinobj.nangle=len(angle)
  454.     dinobj.currentangle=0
  455.     dinobj.prev_while_rot_sound=""
  456.     dinobj.next_while_rot_sound=while_rot_sound[1]
  457.     dinobj.last_rot=0
  458.     RotateObject(dinobj, angle[0], init_w[0], end_w[0], center[0], axis[0], rotation_type, init_rot_sound[0], while_rot_sound[0], end_rot_sound[0], WhileRotFunc, WhileRotArgs, NRotation, (dinobj, angle, init_w, end_w, center, axis, init_rot_sound, while_rot_sound, end_rot_sound))
  459.  
  460.  
  461. def CreateDinamicObject(obj_name):
  462.  
  463.     dinobj=DinObj()
  464.     dinobj.obj=Bladex.GetEntity(obj_name)
  465.     InitDataField.Initialise(dinobj.obj)
  466.     dinobj.obj.Data.dinobjdata=dinobj
  467.     return dinobj
  468.